/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sv.com.intesal.general.controlador;

import sv.com.intesal.general.bean.Banco;
import java.util.ArrayList;
import java.util.List;
import java.sql.ResultSet;
import sv.com.intesal.clases.abstracts.ControladorAbstract;
import sv.com.intesal.general.sql.SQLBanco;

/**
 *
 * @author ejerez
 */
public class BancoControlador extends ControladorAbstract {

    public void insert(Banco b) throws Exception {
        try {
            super.setPreparedStatement(SQLBanco.insert);
            super.ps.setString(1, b.getNombre_b());
            super.ps.setString(2, super.v.getEstado(b.getEstado_filtro_b()));
            super.ps.executeUpdate();

        } catch (Exception e) {
            throw new Exception(e);
        } finally {
            super.setCloseConexion();
        }
    }

    public void update(Banco b) throws Exception {
        try {
            super.setPreparedStatement(SQLBanco.update);
            super.ps.setString(1, b.getNombre_b());
            super.ps.setString(2, super.v.getEstado(b.getEstado_filtro_b()));
            super.ps.setInt(3, b.getCodigo_b());
            super.ps.executeUpdate();
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
            super.setCloseConexion();
        }
    }

    public List<Banco> getBancos() throws Exception {
        List<Banco> lista = new ArrayList<Banco>();
        try {
            this.getExecuteQuery(SQLBanco.getBancos);
            while (super.rs.next()) {
                lista.add(this.getBanco(super.rs));
            }
            return lista;
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
            super.setCloseConexion();
        }
    }

    public List<Banco> getBancosActivos() throws Exception {
        List<Banco> lista = new ArrayList<Banco>();
        try {
            this.getExecuteQuery(SQLBanco.getBancoActivos);
            while (super.rs.next()) {
                lista.add(this.getBanco(super.rs));
            }
            return lista;
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
            super.setCloseConexion();
        }
    }

    private Banco getBanco(ResultSet rs) throws Exception {
        try {
            Banco b = new Banco();
            b.setCodigo_b(rs.getInt("codigo_b"));
            b.setNombre_b(rs.getString("nombre_b"));
            b.setEstado_b(rs.getString("estado_b"));
            b.setEstado_filtro_b(v.getEstado(rs.getString("estado_b")));
            return b;
        } catch (Exception e) {
            throw new Exception(e);
        }
    }
}
